package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.enums.BusinessType;
import com.jsb.laundry.model.dto.CapitalAccountUnstLogListRequest;
import com.jsb.laundry.model.dto.MyInternalPushRequest;
import com.jsb.laundry.model.dto.UpdUserInfoRequest;
import com.jsb.laundry.model.vo.*;
import com.jsb.laundry.service.RiderUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Api(tags = {"我的信息"})
@RestController
@RequestMapping("/myInfo/")
public class MyInfoController {
    @Autowired
    private RiderUserInfoService riderUserInfoService;

    @ApiOperation(value = "我的内推", notes = "我的内推", httpMethod = "POST")
    @PostMapping("myInternalPush")
    public WebResponse myInternalPush() {
        List<FindByPromotionVo> byPromotion = riderUserInfoService.findByPromotion();
        return WebResponse.successData(byPromotion);
    }

    @ApiOperation(value = "我的团队", notes = "我的团队", httpMethod = "POST")
    @PostMapping("teamCentre")
    public WebResponse teamCentre(@RequestBody JSONObject object) {
        String userId = object.getString("userId");
        List<FindByPromotionListVo> byPromotionList = riderUserInfoService.findByPromotionList(userId);
        FindByParentIdVo byParentId = riderUserInfoService.findByParentId(userId);
        if ("0".equals(byParentId.getUserLevel())) {
            byParentId.setUserLevel("专员");
        }
        if ("1".equals(byParentId.getUserLevel())) {
            byParentId.setUserLevel("组长");
        }
        if ("2".equals(byParentId.getUserLevel())) {
            byParentId.setUserLevel("主管");
        }
        if ("3".equals(byParentId.getUserLevel())) {
            byParentId.setUserLevel("总监");
        }

        FindByParentIdVo byParentIds = riderUserInfoService.findByParentId(byParentId.getParentId());
        if (!JSBUtil.isEmptyObject(byParentIds)) {
            if ("0".equals(byParentIds.getUserLevel())) {
                byParentIds.setUserLevel("专员");
            }
            if ("1".equals(byParentIds.getUserLevel())) {
                byParentIds.setUserLevel("组长");
            }
            if ("2".equals(byParentIds.getUserLevel())) {
                byParentIds.setUserLevel("主管");
            }
            if ("3".equals(byParentIds.getUserLevel())) {
                byParentIds.setUserLevel("总监");
            }
        }

        HashMap map = new HashMap();
        map.put("byPromotionList", byPromotionList);
        map.put("byParentId", byParentId);
        map.put("parentId", byParentIds);
        return WebResponse.successData(map);
    }

    @ApiOperation(value = "我的客户", notes = "我的客户", httpMethod = "POST")
    @PostMapping("myClient")
    public WebResponse myClient(@RequestBody MyInternalPushRequest myInternalPushRequest) {
        PageHelper.startPage(myInternalPushRequest.getPageNum(), myInternalPushRequest.getPageSize());
        List<FindByMyCustomVo> byMyCustom = riderUserInfoService.findByMyCustom(myInternalPushRequest.getUserId());
        return WebResponse.successData(byMyCustom);
    }

    @ApiOperation(value = "我的等级", notes = "我的等级", httpMethod = "POST")
    @PostMapping("myLevel")
    public WebResponse myLevel(@RequestBody JSONObject object) {
        FindByParentIdVo userInfo = riderUserInfoService.findByParentId(object.getString("userId"));
        if ("0".equals(userInfo.getUserLevel())) {
            userInfo.setUserLevel("专员");
        }
        if ("1".equals(userInfo.getUserLevel())) {
            userInfo.setUserLevel("组长");
        }
        if ("2".equals(userInfo.getUserLevel())) {
            userInfo.setUserLevel("主管");
        }
        if ("3".equals(userInfo.getUserLevel())) {
            userInfo.setUserLevel("总监");
        }
        //没写
        double money = 0.00;
        HashMap map = new HashMap();
        map.put("userInfo", userInfo);
        map.put("money", money);
        return WebResponse.successData(map);
    }

    @ApiOperation(value = "站点列表", notes = "站点列表", httpMethod = "POST")
    @PostMapping("siteList")
    public WebResponse<List<ShopsSiteListVo>> siteList() {
        List<ShopsSiteListVo> bySite = riderUserInfoService.findBySite();
        return WebResponse.successData(bySite);
    }

    @ApiOperation(value = "查看用户的绑定站点", notes = "查看用户的绑定站点", httpMethod = "POST")
    @PostMapping("userSite")
    public WebResponse userSite(@RequestBody JSONObject object) {
        String userId = object.getString("userId");
        String userSiteInfo = riderUserInfoService.userSite(userId);
        return WebResponse.successData(userSiteInfo);
    }

    @ApiOperation(value = "修改个人信息", notes = "修改个人信息", httpMethod = "POST")
    @PostMapping("updUserInfo")
    public WebResponse updUserInfo(@RequestBody UpdUserInfoRequest userInfoRequest) {
        int i = riderUserInfoService.updUserInfo(userInfoRequest);
        return WebResponse.successData(i);
    }

    @ApiOperation(value = "查询骑手关注公众号", notes = "查询骑手关注公众号", httpMethod = "POST")
    @PostMapping("findByFollow")
    public WebResponse findByFollow(@RequestBody JSONObject jsonObject) {
        String userId = jsonObject.getString("userId");
        int byFollow = riderUserInfoService.findByFollow(userId);
        return WebResponse.successData(byFollow);
    }

    @ApiOperation(value = "待结算列表", notes = "待结算列表", httpMethod = "POST")
    @PostMapping("capitalAccountUnstLogList")
    public WebResponse capitalAccountUnstLogList(@RequestBody CapitalAccountUnstLogListRequest capitalAccountUnstLogListRequest) {
        PageHelper.startPage(capitalAccountUnstLogListRequest.getPageNum(), capitalAccountUnstLogListRequest.getPageSize());
        List<CapitalAccountUnstLogListVo> capitalAccountUnstLogListVos = riderUserInfoService.capitalAccountUnstLogList(capitalAccountUnstLogListRequest.getUserId());
        for (CapitalAccountUnstLogListVo capitalAccountUnstLogListVo : capitalAccountUnstLogListVos) {
            if (JSBUtil.isEmptyObject(capitalAccountUnstLogListVo)) {
                return WebResponse.successData(null);
            }
            if ("in".equals(capitalAccountUnstLogListVo.getType())) {
                capitalAccountUnstLogListVo.setType("+");
            } else if ("out".equals(capitalAccountUnstLogListVo.getType())) {
                capitalAccountUnstLogListVo.setType("-");
            }
            capitalAccountUnstLogListVo.setBusinessType(BusinessType.getNameById(capitalAccountUnstLogListVo.getRemark()));
            capitalAccountUnstLogListVo.setAmount(JSBUtil.turnDoubleStrNew(Double.parseDouble(capitalAccountUnstLogListVo.getAmount())));
        }
        return WebResponse.successData(capitalAccountUnstLogListVos);
    }

    @ApiOperation(value = "已结算列表", notes = "已结算列表", httpMethod = "POST")
    @PostMapping("capitalAccountLogList")
    public WebResponse capitalAccountLogList(@RequestBody CapitalAccountUnstLogListRequest capitalAccountUnstLogListRequest) {
        PageHelper.startPage(capitalAccountUnstLogListRequest.getPageNum(), capitalAccountUnstLogListRequest.getPageSize());
        List<CapitalAccountUnstLogListVo> capitalAccountUnstLogListVos = riderUserInfoService.capitalAccountLogList(capitalAccountUnstLogListRequest.getUserId());
        for (CapitalAccountUnstLogListVo capitalAccountUnstLogListVo : capitalAccountUnstLogListVos) {
            if (JSBUtil.isEmptyObject(capitalAccountUnstLogListVo)) {
                return WebResponse.successData(null);
            }
            if ("in".equals(capitalAccountUnstLogListVo.getType())) {
                capitalAccountUnstLogListVo.setType("+");
            } else if ("out".equals(capitalAccountUnstLogListVo.getType())) {
                capitalAccountUnstLogListVo.setType("-");
            }
            capitalAccountUnstLogListVo.setBusinessType(BusinessType.getNameById(capitalAccountUnstLogListVo.getRemark()));
            capitalAccountUnstLogListVo.setAmount(JSBUtil.turnDoubleStrNew(Double.parseDouble(capitalAccountUnstLogListVo.getAmount())));
        }
        return WebResponse.successData(capitalAccountUnstLogListVos);
    }

    @ApiOperation(value = "今日单量今日收入", notes = "今日单量今日收入", httpMethod = "POST")
    @PostMapping("todayMoney")
    public WebResponse todayMoney(@RequestBody JSONObject object) {

        String userId = object.getString("userId");
        String today = JSBUtil.newDate(0);
        String todayBak = JSBUtil.newDate(1);
        String todayBaks = JSBUtil.newDate(2);
        //当天金额
        int todayMoney = riderUserInfoService.todayMoney(userId, today, today);

        String turnDoubleStr = JSBUtil.turnDoubleStrNew(todayMoney);
        int todayNumber = riderUserInfoService.todayNumber(userId, today, today);
        //昨天金额
        int todayMoneyBak = riderUserInfoService.todayMoney(userId, todayBak, todayBak);

        double v2 = todayMoney - todayMoneyBak;
        String turnDoubleStrs = JSBUtil.turnDoubleStrNew(v2);

        int todayNumberBak = riderUserInfoService.todayNumber(userId, todayBak, todayBak);

        int j = todayNumber - todayNumberBak;
        HashMap map = new HashMap();
        map.put("todayMoney", turnDoubleStr);
        map.put("todayNumber", todayNumber);
        map.put("todayNumberBak", j);
        map.put("todayMoneyBak", turnDoubleStrs);
        return WebResponse.successData(map);
    }
    @ApiOperation(value = "性别查询", notes = "性别查询", httpMethod = "POST")
    @PostMapping("forIdSex")
    public WebResponse forIdSex(@RequestBody JSONObject object){
        String userId = object.getString("userId");
        String byIdNumber = riderUserInfoService.findByIdNumber(userId);
        String sex = JSBUtil.idCardNumberForName(byIdNumber);
        return WebResponse.successData(sex);

    }

}
